package com.wyj.http;

import java.io.IOException;
import java.io.InputStream;

/**
 * TODO: socket信息封装类
 *
 * @author wuyijie
 * @date 2021/12/12 10:14
 */
public class Request {
    private InputStream inputStream;
    private String url;
    /**
     * get/post
     */
    private String methodType;

    public Request(InputStream inputStream) {
        this.inputStream = inputStream;

        try {
            //从输入流读取信息
            int count = 0;
            while (count == 0) {
                count = inputStream.available();
            }
            byte[] bytes = new byte[count];
            int read = inputStream.read(bytes);
            String readStr = new String(bytes);
            //获取输入流第一行的内容
            String firstLineStr = readStr.split("\\n")[0];
            String methodType = firstLineStr.split(" ")[0];
            String url = firstLineStr.split(" ")[1];
            this.methodType = methodType;
            this.url = url;

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public String getUrl() {
        return url;
    }

    public String getMethodType() {
        return methodType;
    }

    @Override
    public String toString() {
        return "Request{" +
                "inputStream=" + inputStream +
                ", url='" + url + '\'' +
                ", methodType='" + methodType + '\'' +
                '}';
    }
}
